<?php

require_once ("../conf/conf.php");
require ("../include/constants.php");
require ("../include/function.php");

define('TOC_LINKS', '<a href="#toc-old-testament">[VT]</a> | <a href="#toc-new-testament">[NT]</a>');
define('PAGEBREAK', "<p><mbp:pagebreak/></p>\n");

$connection = db_connect(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

session_start();
?>

<html>
<head> 
	<title>Biblia</title> 
	<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/> 
	<style> 
		 p{ text-indent: 0px; margin: 5px 0;} 
		 .title {font-weight: bold; font-style:italic; text-align: center;}
		 #toc p, #toc h3{ text-indent: 25px; margin: 15px 0;} 
		 #toc p.index {font-size: 1.4em;}
		 #toc .toc1{ text-indent: 5px;}
        .links {font-size: 1.4em; font-weight: bold;}
	</style> 
</head> 
<body>

<p id="start">Biblia</p>

<div class="toc" name="toc" id="toc"> 

<?php
	$TOC;
	echo getTOC();
?>

</div> 
<p><mbp:pagebreak/></p> 

<a name="old-testament"></a>

<?php

generateBooks();

?>

</body>
</html>

<?php
// ====================================================

function getTOC(){
	global $TOC;
	$sql = "SELECT id, name, seo_name, (SELECT max(number) FROM chapter WHERE book_id = book.id) as chapters FROM book";
    $TOC = sql2JSONObject($sql);
	$html = '';
	$html = $html . '<h2 class="toc1 links"><a name="toc-old-testament"></a>Vechiul Testament</h2>' . "\n";
	$html = $html . '<p class="index">' . "\n";
	foreach($TOC as $book) {
		if($book['id'] == 40){
			$html = $html . "\n</p>\n";
            $html = $html . PAGEBREAK;
			$html = $html . '<h2 class="toc1 links"><a name="toc-new-testament"></a>Noul Testament</h2>' . "\n";
			$html = $html . '<p class="index">' . "\n";
		}
		$html = $html . '<a href="#' . $book['seo_name'] . '">' . $book['name'] . '</a>';
        if($book['id'] != 39){
            $html = $html . ' | ';
        }
	}
	$html = substr($html, 0, -2);
	$html = $html . "\n</p>\n";
    return $html;
}

function generateBooks(){
	global $TOC;
	
	foreach($TOC as $book) {
//		if($book['id'] == 3){
//			return;
//		}
		if($book['id'] == 40){
			echo '<a name="new-testament"></a>';
			echo '<h1>Noul Testament</h1>';
			echo PAGEBREAK;
		}

        $chapterTOC = '<p><a name="' . $book['seo_name'] . '"></a><h2>' . TOC_LINKS . ' | ' . $book['name'] . '</h2></p>';
        $chapterTOC = $chapterTOC . "<p class=\"links\">\n| ";
		for ($j=1; $j <= $book['chapters']; $j++){
			$chapterTOC = $chapterTOC . '<a href="#'. $book['seo_name'] . "-$j\">&nbsp;$j&nbsp;</a> | ";
		}
		$chapterTOC = substr($chapterTOC, 0, -1);
		$chapterTOC = $chapterTOC . "</p>\n";
		$chapterTOC = $chapterTOC . PAGEBREAK;
		echo $chapterTOC;
		
		for ($i=1; $i <= $book['chapters']; $i++){
			echo getChapter($book['id'], $i);
			echo PAGEBREAK;
		}
	}
}


function getChapter($bookId, $chapter){
	global $TOC;
    $ref = false;

	$sql = "SELECT verse.id, verse.number, text ";
    $sql = $sql.", (SELECT text FROM titluri WHERE id_verset = verse.id) AS title ";

    if($ref) {
        $sql = $sql.", (SELECT GROUP_CONCAT(CONCAT('#',id_trimitere,'\">', text, '$') SEPARATOR '; ') ".
                "FROM trimiteri WHERE id_verset = verse.id GROUP BY id_verset) AS ref ";
    } else {
        $sql = $sql.", '' AS ref ";
    }

	$sql = $sql."FROM verse JOIN chapter ON (verse.chapter_id = chapter.id) ";

    $sql = $sql."WHERE book_id = $bookId AND chapter.number = $chapter ";

	$book = $TOC[$bookId-1];
	$html = '<p><a name="' . $book['seo_name'] . "-$chapter\"></a><h3>" . TOC_LINKS . ' | <a href="#'. $book['seo_name'] .'">' . $book['name'] . "</a> $chapter</h3></p>\n";
	return $html . getHtmlChapter($sql, $book, $chapter);
}



function getHtmlChapter($query, $book, $chapter) {
    $result = do_query($query);
	$html = '';
	if($total = mysql_num_rows($result)) {
        while ($data = mysql_fetch_row($result)) {
			if($data[3]){
				//$ref = "<sup>(<a href=\"#" . $book['seo_name'] . "-$chapter\">" . $book['name'] . " $chapter</a>)</sup>";
				$ref = "<sup>(" . $book['name'] . " $chapter)</sup> ";
				$html = $html . "<p class=\"title\">$ref" . $data[3] . "</p>\n";
			}
			$vnumber = ($data[1] % 5 == 0 ? "$chapter:" : '') . $data[1];
			$html = $html . "<p><sup>$vnumber</sup> " . $data[2] . "</p>\n";
		}
    }
    return $html;
}


db_disconnect($connection);
?>